package gmew.server.entities;

import gmew.shared.ItemDTO;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Text;

@PersistenceCapable
public class Item {
	@SuppressWarnings("unused")
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
	private String encodedKey;

	@Persistent
	@Extension(vendorName="datanucleus", key="gae.pk-name", value="true")
	private String id;
	
	@Persistent
	private Text rawXml;
	
	@Persistent
	private String name;

	public Item() {
		
	}
	
	public Item(ItemDTO dto) {
		setId(dto.getId());
		setName(dto.getName());
		setRawXml(dto.getRawXml());
	}
	
	public int getId() {
		return Integer.parseInt(id);
	}

	public void setId(int id) {
		this.id = Integer.toString(id);
	}

	public String getRawXml() {
		return rawXml.getValue();
	}

	public void setRawXml(String rawXml) {
		this.rawXml = new Text(rawXml);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public ItemDTO toItemDTO() {
		ItemDTO dto = new ItemDTO();
		dto.setId(getId());
		dto.setName(name);
		if (rawXml != null) dto.setRawXml(rawXml.getValue());
		return dto;
	}
}
